package com.atung.parking.controller;


import com.atung.parking.config.permission.PermissionValid;
import com.atung.parking.config.result.ApiResult;
import com.atung.parking.config.result.PageInfoResponse;
import com.atung.parking.config.result.PageResponse;
import com.atung.parking.entity.BusParkingIotFranchise;
import com.atung.parking.entity.BusParkingIotPackage;
import com.atung.parking.enums.AdminType;
import com.atung.parking.enums.MessageType;
import com.atung.parking.mapstruct.ParkingIotFranchiseConverter;
import com.atung.parking.request.query.QueryFranchiseRequest;
import com.atung.parking.request.query.QueryMessageRequest;
import com.atung.parking.request.query.QueryParkingIotRequest;
import com.atung.parking.request.save.SaveOrUpdateFranchiseRequest;
import com.atung.parking.response.page.FranchiseResponse;
import com.atung.parking.response.page.MessagePublishResponse;
import com.atung.parking.response.page.ParkingIotResponse;
import com.atung.parking.service.ParkingIotFranchiseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wlt
 */
@RestController
@Api(tags = "车场加盟管理")
@RequestMapping("/api/franchise")
public class ParkingIotFranchiseController {

    @Resource
    private ParkingIotFranchiseService parkingIotFranchiseService;
    @Resource
    private ParkingIotFranchiseConverter parkingIotFranchiseConverter;

    @PostMapping("/app/save")
    @ApiOperation("(小程序)新增加盟信息")
    @PermissionValid(roleType = {AdminType.USER, AdminType.SUPERADMIN})
    public ApiResult saveParkingIotFranchise(@RequestBody @Valid SaveOrUpdateFranchiseRequest request) {
        return parkingIotFranchiseService.saveParkingIotFranchiseService(request);
    }

    @GetMapping("/query")
    @ApiOperation("获取加盟列表")
    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public ApiResult<PageResponse<FranchiseResponse>> queryParkingIotFranchise(QueryFranchiseRequest request) {
        PageInfoResponse<BusParkingIotFranchise> pageInfoResponse = parkingIotFranchiseService.queryParkingIotFranchiseService(request);
        return ApiResult.ok(pageInfoResponse.getData().stream().map(parkingIotFranchiseConverter::toResponse)
                .collect(Collectors.toList()), request.getPageNum(), request.getPageSize(), pageInfoResponse.getTotal());
    }

    @GetMapping("/app/queryOneself")
    @ApiOperation("（小程序）获取已发布的加盟列表")
    @PermissionValid(roleType = {AdminType.USER})
    public ApiResult<List<FranchiseResponse>> queryOneselfParkingIotFranchiseList() {

        List<FranchiseResponse> franchiseResponses = parkingIotFranchiseService.queryOneselfParkingIotFranchiseListService
                ();

        return ApiResult.ok(franchiseResponses);
    }

    @GetMapping("/export")
    @ApiOperation("导出车场加盟列表")
//    @PermissionValid(roleType = {AdminType.SUPERADMIN})
    public void exportParkingIotFranchise(QueryFranchiseRequest request, HttpServletResponse httpServletResponse) {
        parkingIotFranchiseService.exportParkingIotFranchiseService(request, httpServletResponse);
    }

}
